package cz.pproproj.aaservice.dldapmodule;

import cz.pproproj.aaservice.aasbllmodule.authentication.AuthenticationUser;
import cz.pproproj.aaservice.guimodule.model.AuthenticationException;
import org.springframework.ldap.core.support.LdapContextSource;

/**
 * TODO remove this class
 * Created by Radek on 16.1.2015.
 */

public class Main {

    public static void main(String [] args) {

        LdapAuthenticationDataAccessImpl ldapUsersDataAccess = new LdapAuthenticationDataAccessImpl("ldap1");
        try {
            ldapUsersDataAccess.authenticate(new AuthenticationUser("einstein", "password"));
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl("ldap://ldap.forumsys.com:389");
        contextSource.setBase("dc=example,dc=com");
        contextSource.afterPropertiesSet();

        LdapContextSource contextSource2 = new LdapContextSource();
        contextSource2.setUrl("ldap://ldap.testathon.net:389");
        contextSource2.setBase("DC=testathon,DC=net");
        contextSource2.afterPropertiesSet();

        AuthenticationManager manager = new AuthenticationManager(contextSource);
        AuthenticationManager manager2 = new AuthenticationManager(contextSource2);

        Credentials credentials = new Credentials();
        credentials.setGroup("mathematicians");
        credentials.setUser("riemann");
        credentials.setPassword("password");

        Credentials credentials2 = new Credentials();
        credentials2.setGroup("mathematicians");
        credentials2.setUser("einstein");
        credentials2.setPassword("password");

        Credentials credentials3 = new Credentials();
        credentials3.setUser("john");
        credentials3.setPassword("john");

        String [] members = null;
        try {
            System.out.println(credentials);
            System.out.println(manager.authenticate(credentials));

            System.out.println(credentials2);
            System.out.println(manager.authenticate(credentials2));

            System.out.println(credentials3);
            System.out.println(manager2.authenticate(credentials3));

            members = manager.findLdapGroupUsers(credentials.getGroup());

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Members of group " + credentials.getGroup());
        for (String m : members) {
            System.out.println(m);
        }

        System.out.println("User " + credentials.getUser() + " is member of group " + credentials.getGroup());
        System.out.println(manager.memberOfGroup(credentials.getUser(), credentials.getGroup()));

        System.out.println("User " + credentials2.getUser() + " is member of group " + credentials2.getGroup());
        System.out.println(manager.memberOfGroup(credentials2.getUser(), credentials2.getGroup()));

    }

}
